package com.redxiii.safesync;

import java.io.File;
import java.io.IOException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PatternOptionBuilder;
import org.apache.commons.cli.PosixParser;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.FileConfiguration;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class SyncHandlerMain {

	private static Logger logger = LoggerFactory.getLogger(SyncHandlerMain.class);
	private static Options options = new Options();
	private static FileConfiguration config;
	private static boolean isValidConfig;
	
	@SuppressWarnings("static-access")
	private static void init() {
		options.addOption("help", false, "help");
		options.addOption("p", true, "Password");
		options.addOption("l", true, "Local folder");
		options.addOption("r", true, "Remote folder");
		options.addOption(OptionBuilder.withArgName("p").hasArg().withType(PatternOptionBuilder.STRING_VALUE).withDescription("password").create("p"));
	}
	
	private static void loadConfiguration() {
		try {
			File preferences = new File("preferences.properties");
			
			if (preferences.exists()) {
				config = new PropertiesConfiguration(preferences);
				
				checkConfiguration();
			} else {
				preferences.createNewFile();
				config = new PropertiesConfiguration(preferences);
			}
			
		} catch (ConfigurationException e) {
			logger.error("Fail creating configuration file",e);
		} catch (IOException e) {
			logger.error("Fail creating configuration file",e);
		}
	}
	
	private static void checkConfiguration() {
		
		isValidConfig = false;
		if (config != null) {
			File local = new File(config.getString("local"));
			File remote = new File(config.getString("remote"));
			
			if (!local.exists() || !local.isDirectory()) {
				logger.warn("Invalid local directory");
				config.setProperty("local", "");
				return;
			}
			
			if (!remote.exists() || !remote.isDirectory()) {
				logger.warn("Invalid remote directory");
				config.setProperty("remote", "");
				return;
			}
			
			SyncDirection direction = null;
			try {
				direction = SyncDirection.valueOf(config.getString("direction",null));
			} catch (Exception e) {
				config.setProperty("direction", null);
			}
			if (direction == null) {
				logger.warn("Invalid direction");
				return;
			}
			
			isValidConfig = true;
		}
	}
	
	private static CommandLine parseArgs(String[] args) {
		CommandLineParser parser = new PosixParser();
		
		try {
			return parser.parse(options, args);
		
		} catch (ParseException e) {
			logger.error("Parsing failed.  Reason: " + e.getMessage());
			HelpFormatter formatter = new HelpFormatter();
			formatter.printHelp("java", options);
		}
		return null;
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		init();
		loadConfiguration();
		
		if (args.length > 0) {
			CommandLine line = parseArgs(args);
			
			if (line.hasOption("help")){
				new HelpFormatter().printHelp("java", options);
			} else if(isValidConfig && 
					line.getOptionObject('p') != null && 
					line.getOptionObject('l') == null && 
					line.getOptionObject('r') == null) {
				// Run from configuration
			} else if (line.getOptionObject('p') != null && 
					line.getOptionObject('l') != null &&
					line.getOptionObject('r') != null) {
				
				config.setProperty("local", line.getOptionObject('l'));
				config.setProperty("remote", line.getOptionObject('r'));
				// Run from command line
			} else {
				
			}
		} else {
			//Graphical
		}
	}

}
